import {reactive, ref} from 'vue'
import pinia from '@/stores/index.js';
import {useUserStore} from '@/stores/counter.js'

const userStore = useUserStore(pinia)
const user_info= reactive({
	userid:String,
	openid:String,
	name:String,
	dp:String
})
class Utils {
	constructor(){
		//接口地址前缀
		this.baseUrl = "http://192.168.0.115:3000"
	}
	
	
	isLogin(){
		const login_id = uni.getStorageSync('user_info')
		
		if(login_id ==''){
			uni.navigateTo({
				url:'/pages/login/index'
			})
		}
	}
	
	getUserInfo(){
		//调用登录的api
		uni.showModal({
			title:"登录获取权限提醒",
			content:'是否同意获取权限',
			success:res=>{
				if(res.confirm){				//用户点击确定
					console.log('用户点击确定')
					uni.login({
						provider:'weixin',
						success:res=>{
							console.log(res)
							uni.request({
								url:'https://api.weixin.qq.com/sns/jscode2session',
								method:'GET',
								data:{
									appid:'wx1b16061fc883d6d7',
									secret:'b45c4681d256ebf5337f62e1c3d291bf',
									js_code:res.code,
									grant_type:'authorization_code'
								},
								success:res =>{
									console.log(res,'res')
									user_info.openid = res.data.openid
									this.request({								//服务器储存
										url:'/openidStorage',
										data:{
											openid:user_info.openid
										},
										success:res=>{
											user_info.userid =res[0].userid
											user_info.dp =res[0].dp
											user_info.name =res[0].name
											console.log(user_info)
											userStore.fillUser(user_info)              //pinia储存
											uni.setStorageSync("user_info",user_info)  //本地储存
										},
									})
									uni.showToast({
										title:'登录成功',
										icon:'none'
									})
									setTimeout(()=>{
										uni.switchTab({
											url:"/pages/collect/index"
										})
									},1000)
								},
								fail:res =>{
									console.log(res,'res')
								}
							})
						}
					})
				}else if (res.cancel){  //用户点击取消
					console.log('用户点击取消')
					uni.showToast({
						title:'获取失败',
						icon:'none'
					})
				}
			}
		})
	}
	//封装请求
	request(option={
		showLoading:false
	}){
		//判断url是否存在
		if(!option.url){
			return false
		}
		if(option.showLoading){
			this.showLoading()
		}
		uni.request({
			url:this.baseUrl + option.url,
			data:option.data ? option.data:{},
			header:option.header ? option.header:{},
			method:option.method ? option.method:'GET',
			success:(response) => {
				uni.hideLoading()
				//后端返回的数据异常
				if(response.statusCode !==200){
					if(option.fail && typeof option =='function'){
						option.fail(response)
					}
				}else{
					if(option.success && typeof option.success =="function"){
						option.success(response.data)
					}
				}
				
			},
			fail:(response)=>{
				uni.hideLoading()
				console.log(response)
			}
			
		})
	}
	
	//创建加载的loading效果
	showLoading(){
		const isShowLoading = uni.getStorageSync('isShowLoading')
		if(isShowLoading){
			uni.hideLoading()
			uni.setStorageSync('isShowLoading',false)
		}
		uni.showLoading({
			title:'加载中...',
			complete:function(){
				uni.setStorageSync('isShowLoading',true)
			},
			fail:function(){
				uni.setStorageSync('isShowLoading',false)
			}
		})
	}
	

	toDetails(hid){
			uni.navigateTo({
				url:'/pages/music_detail/index?hid='+ hid
			})
		}
		
	toVideos(hid,vid){
			uni.navigateTo({
				url:'/pages/video/index?hid='+hid+'&vid=' + vid
			})
		}
}

export default new Utils
